from python_ai.common.xcommon import *
import numpy as np
import matplotlib.pyplot as plt
import datetime

spr = 2
spc = 3
spn = 0
plt.figure(figsize=[9, 6])


def my_show_img(img, title="no title", trans=None, **kwargs):
    global spn
    spn += 1
    plt.subplot(spr, spc, spn)
    if trans is not None:
        img = trans(img)
    plt.imshow(img, **kwargs)
    # plt.axis('off')
    plt.title(title)


S = 6
mat1 = np.full((S, S), 10)
mat2 = np.full((S, S), 20)
mat12 = np.concatenate((mat1, mat2), axis=1)
mat21 = np.concatenate((mat2, mat1), axis=1)
mat = np.concatenate((mat12, mat21), axis=0)
print(mat)
my_show_img(mat, 'ori', cmap='gray')

kernels = [
    np.ones((3, 3)),
    np.float32([[-1, 0, 1],
     [-1, 0, 1],
     [-1, 0, 1]]),
    np.float32([[-1, -1, -1],
     [0, 0, 0],
     [1, 1, 1]]),
    np.float32([[0, -1, 0],
     [-1, 5, -1],
     [0, -1, 0]]),
    np.float32([[1, 1, 1, 1, 1],
     [1, 0, 0, 0, 1],
     [1, 0, 0, 0, 1],
     [1, 0, 0, 0, 1],
     [1, 1, 1, 1, 1]])
]
dt1 = datetime.datetime.now()
for i, k in enumerate(kernels):
    print(k)
    res = convolution_numpy(mat, k)
    print(res)
    my_show_img(res, f'res#{i}', cmap='gray')
dt2 = datetime.datetime.now()
print(dt2 - dt1)
